package com.bugpool.leilema.tc.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.hutool.core.lang.tree.Tree;
import com.bugpool.leilema.freamwork.satoken.SaTokenUtil;
import com.bugpool.leilema.tc.bean.entity.SysUser;
import com.bugpool.leilema.tc.bean.interfaces.UserConvert;
import com.bugpool.leilema.tc.bean.res.UserRes;
import com.bugpool.leilema.tc.bean.vo.SysUserVo;
import com.bugpool.leilema.tc.mapper.TreeMapper;
import com.bugpool.leilema.tc.service.LoginService;
import com.bugpool.leilema.tc.bean.req.LoginReq;
import com.bugpool.leilema.tc.service.MenuService;
import com.bugpool.leilema.tc.service.TreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author wh
 * @Description
 * @Date 2023/3/1 14:33
 * @Version 1.0
 **/
@RestController
@Api(tags = "登录控制器")
public class LoginController extends BaseController {

    @Resource
    private LoginService loginService;

    @Resource
    private MenuService menuService;

    @Resource
    private TreeService treeService;

    @Resource
    private TreeMapper treeMapper;


    @PostMapping("/login")
    @ApiOperation("登录接口")
    public SaTokenInfo login(@RequestBody LoginReq loginReq) {
        return loginService.login(loginReq);
    }

    @GetMapping("/getInfo")
    @ApiOperation("获取用户详情接口")
    public UserRes getInfo() {
        SysUserVo user = SaTokenUtil.getUser();
        return UserConvert.INSTANCE.convertToRes(user);
    }

    @GetMapping("/getRouters")
    @ApiOperation("获取路由接口")
    public List<Tree<Long>> getRouters() {
        //获取用户id
        List<Tree<Long>> sysMenuList = menuService.getMenuListByUser();
        return sysMenuList;
    }

    @GetMapping("/getLayerTree")
    @ApiOperation("获取树形结构树")
    public List<Tree<Long>> getDataTree() {
        //获取用户信息
        SysUserVo user = SaTokenUtil.getUser();
        if (user.isAdmin()) {
            //查询所有的树
            return treeService.listTree();
        }
        //根据用户信息查询出部分树
        return treeService.listTreeByUserId(user.getUserId());
    }

    @GetMapping("/getPermissions")
    @ApiOperation("获取权限集合")
    public Set<String> getPermissions() {
        return menuService.getPermissions();
    }






}
